package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.graphics.Paint
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityAllReserveMemberInfoBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterAllReserveMemberInfo
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.kingja.loadsir.core.LoadService
import com.luck.picture.lib.entity.MediaData
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/6/22 10:11
 * @Describe    : 所有会员预定信息
 */
class ActivityAllReserveMemberInfo : BaseActivity<OrgDetailViewModel, ActivityAllReserveMemberInfoBinding>() {

    private lateinit var mAdapter: AdapterAllReserveMemberInfo
    private var mRaceId = 0
    private lateinit var loadsir: LoadService<Any>

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        mRaceId = intent.getIntExtra("raceId", 0)
        val orgName = intent.getStringExtra("orgName")
        val raceName = intent.getStringExtra("raceName")
        mDatabind.includeBar.tvTitle.text = "$orgName$raceName" + getString(R.string.Membership_reservation)

        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(true)
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        onRefresh(true)

        mAdapter = AdapterAllReserveMemberInfo(arrayListOf())
        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, 0))
            it.initFooter {
                //触发加载更多时请求数据
                onRefresh(false)
            }
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    onRefresh(true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.allMemberResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.includeSearch.ivClose, mDatabind.includeSearch.tvSearch) {
            when (it) {
                mDatabind.includeSearch.tvSearch -> {
                    onRefresh(true)
                }

                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    onRefresh(true)
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }

    private fun onRefresh(isRefresh: Boolean) {
        val name = mDatabind.includeSearch.editQuery.text.toString().trim()
        loadsir.showLoading()
        mViewModel.getMemberReservationList(isRefresh, mRaceId, name, "", 0)
    }
}